/**********************************************************************************
 * Copyright (c) 2012 - 2012,  Collect4PNP, All right reserved
 * Title: com.cipas.pnp.jdbc  PoolHandle.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-7-11 上午10:21:07 
 *********************************************************************************/


package com.adc.jdbc.pool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.adc.quicktools.StringUtil;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;



public class PoolHandle {
	private static Map<String, BoneCP> connectionPoolMap=null;
	
	/**
	 * 
	 * Description: 初始化
	 * author:  shen
	 * @return PoolHandle
	 */
	public final static PoolHandle getInstance() {
		return new PoolHandle();
	}
	
	/**
	 * 
	 * Description: to get bonecp instance from connectionpoolList ;
	 * author:  shen
	 * @param poolName  
	 * @return BoneCP
	 */
	public synchronized BoneCP getBoneCPByPoolName(String poolName) {
		Map<String, BoneCP> tempMap=new HashMap<String, BoneCP>(getConnectionPoolMap());
		System.out.println("param is:"+poolName);
		if (StringUtil.checkStringValue(poolName)) {
			if (tempMap.containsKey(poolName)) {
				return tempMap.get(poolName);
			}else {
				System.out.println("Pool[name="+poolName+"] is not exist in connectionPoolList!\n");
			}
		}
		return null;
	}
	
	/**
	 * 
	 * Description:  obtain db connection by pool name;
	 * author:  shen
	 * @param poolName
	 * @return Connection
	 */
	public synchronized final Connection getConnection(String poolName) {
		Connection connection=null;
		BoneCP boneCP=getBoneCPByPoolName(poolName);
		if (boneCP!=null) {
			try {
				connection= boneCP.getConnection();
				System.out.println("Successfully obtain the database connection！");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("The db config ["+poolName+".properties] is not exist!");
		}
		return connection;
	}

	/**
	 * 
	 * Description: convert connectionPoolList to connectionPoolMap,the name is poolName;
	 * author:  shen
	 * @return Map<String,BoneCP>
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public synchronized Map<String, BoneCP> getConnectionPoolMap() {
		if (connectionPoolMap==null) {
			connectionPoolMap=new HashMap<String, BoneCP>();
			BoneCP boneCP=null;
			Map<String, BoneCPConfig> map= ConfigMap.getBoneConfigMap();
			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
				Entry<String, BoneCPConfig> entry = (Entry<String, BoneCPConfig>) it.next();
				try {
					boneCP=new BoneCP(entry.getValue());
				} catch (SQLException e) {
					e.printStackTrace();
				}
				connectionPoolMap.put(entry.getKey(), boneCP);
			}
		}
		return connectionPoolMap;
	}

	/**
	 * 
	 * Description: closeoff all pool in connectionpoolList;
	 * author:  shen 
	 * @return void
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public synchronized void closePool() {
		if (connectionPoolMap!=null) {
			for (Iterator iterator = connectionPoolMap.entrySet().iterator(); iterator.hasNext();) {
				Entry<String, BoneCP> entry = (Entry<String, BoneCP>) iterator.next();
				System.out.println("Shuting down[" + entry.getKey() + "]");
				entry.getValue().shutdown();
			}
		}else {
			System.out.println("Can not Close Pools! Static connectionPoolMap is Null!");
		}
	}
}
